package com.wbihua.common.metrics.config;

import com.wbihua.common.metrics.interceptor.PrometheusMetricsInterceptor;
import com.wbihua.common.metrics.type.MeterType;
import com.wbihua.common.metrics.manager.MetricManager;
import com.wbihua.metrics.manager.PrometheusMetricManager;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class AppConfig implements WebMvcConfigurer {

    @Resource
    private PrometheusMeterRegistry meterRegistry;

    @Bean
    @DependsOn("prometheusMeterRegistry")
    public MetricManager metricManager(){
        return new PrometheusMetricManager(new MeterConfig(MeterType.PROMETHEUS), meterRegistry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 全局访问性能拦截
        registry.addInterceptor(new PrometheusMetricsInterceptor(metricManager())).addPathPatterns("/**");
    }
}
